package com.qdxy.app.lhjh.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lf.tempcore.tempActivity.TempActivity;
import com.lf.tempcore.tempAdapter.TempListAdapter;
import com.lf.tempcore.tempModule.tempDebuger.Debug;
import com.lf.tempcore.tempModule.tempRemotComm.TempRemotAPIConnecter;
import com.lf.tempcore.tempViews.TempNestingGridView;
import com.qdxy.app.lhjh.R;
import com.qdxy.app.lhjh.activities.exceptions.ActExceptionDaoJuList;
import com.qdxy.app.lhjh.activities.exceptions.ActExceptionEquList;
import com.qdxy.app.lhjh.activities.exceptions.ActExceptionJiaGongList;
import com.qdxy.app.lhjh.activities.exceptions.ActExceptionJudgementList;
import com.qdxy.app.lhjh.api.TempAPI;
import com.qdxy.app.lhjh.bean.OptionGroupData;
import com.qdxy.app.lhjh.bean.TestBean;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 首页
 * Created by KY on 2016/10/13.
 */

public class ActHome extends TempActivity implements NavigationView.OnNavigationItemSelectedListener {
    private LinearLayout act_home_rootView;
    private DrawerLayout mDrawer;
    private View.OnClickListener mGridViewClickListener;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.act_home_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    protected void findViews() {
        act_home_rootView = (LinearLayout) findViewById(R.id.act_home_rootView);
    }

    @Override
    protected void setListeners() {
        mGridViewClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        };
    }
    @Override
    protected void bindValues() {
        act_home_rootView.removeAllViews();
        List<OptionGroupData> itemData = new ArrayList<>();
        OptionGroupData groupitem1;
        List<OptionGroupData.OptionData> childData1;
        OptionGroupData.OptionData child;

        //group
        groupitem1 = new OptionGroupData();
        groupitem1.setGroupName("生产加工");
        //child
        childData1 = new ArrayList<>();
        child = new OptionGroupData.OptionData();
        child.setImage("http://img0.imgtn.bdimg.com/it/u=1126541908,2603454962&fm=21&gp=0.jpg");
        child.setName("产品加工");
        child.setAction("产品加工");
        childData1.add(child);

        child = new OptionGroupData.OptionData();
        child.setImage("http://img0.imgtn.bdimg.com/it/u=1126541908,2603454962&fm=21&gp=0.jpg");
        child.setName("辅料领用审批");
        child.setAction("辅料领用审批");
        childData1.add(child);


        groupitem1.setOptionData(childData1);
        itemData.add(groupitem1);

        //group
        groupitem1 = new OptionGroupData();
        groupitem1.setGroupName("异常处理");
        //child
        childData1 = new ArrayList<>();

        child = new OptionGroupData.OptionData();
        child.setImage("http://img0.imgtn.bdimg.com/it/u=1126541908,2603454962&fm=21&gp=0.jpg");
        child.setName("异常判断");
        child.setAction("异常判断");
        childData1.add(child);

        child = new OptionGroupData.OptionData();
        child.setImage("http://pic33.nipic.com/20130916/3420027_192919547000_2.jpg");
        child.setName("加工异常");
        child.setAction("加工异常");
        childData1.add(child);

        child = new OptionGroupData.OptionData();
        child.setImage("http://pic33.nipic.com/20130916/3420027_192919547000_2.jpg");
        child.setName("设备异常");
        child.setAction("设备异常");
        childData1.add(child);

        child = new OptionGroupData.OptionData();
        child.setImage("http://pic33.nipic.com/20130916/3420027_192919547000_2.jpg");
        child.setName("刀具异常");
        child.setAction("刀具异常");
        childData1.add(child);

//        child = new OptionGroupData.OptionData();
//        child.setImage("http://img0.imgtn.bdimg.com/it/u=1126541908,2603454962&fm=21&gp=0.jpg");
//        child.setName("辅料领用审批");
//        child.setAction("辅料领用审批");
//        childData1.add(child);


        groupitem1.setOptionData(childData1);
        itemData.add(groupitem1);
//        for (int i = 0; i < 4; i++) {
//            OptionGroupData item = new OptionGroupData();
//            item.setGroupName("group" + i);
//            List<OptionGroupData.OptionData> childData = new ArrayList<>();
//            for (int j = 0; j < 2; j++) {
//                OptionGroupData.OptionData child = new OptionGroupData.OptionData();
//                child.setImage("http://img0.imgtn.bdimg.com/it/u=1126541908,2603454962&fm=21&gp=0.jpg");
//                child.setName("辅料领用审批");
//                child.setAction(""+i+"|"+j);
//                childData.add(child);
//
//            }
//            item.setOptionData(childData);
//            itemData.add(item);
//        }
        for (int i = 0; i < itemData.size(); i++) {
            List<OptionGroupData.OptionData> childList = itemData.get(i).getOptionData();
            if (childList==null||childList.isEmpty()){
                continue;
            }
            initGridData(itemData.get(i));
        }
        TempRemotAPIConnecter.INSTANCE.executeRemotAPI(TempRemotAPIConnecter.INSTANCE.createRemoteApi(TempAPI.class).userLogin("test","test"), new Callback<TestBean>() {
            @Override
            public void onResponse(Call<TestBean> call, Response<TestBean> response) {
                Debug.info("MainActivity", response.body().toString());

            }

            @Override
            public void onFailure(Call<TestBean> call, Throwable t) {
                Debug.info("MainActivity", "获取数据失败" + t.toString());
            }
        });
    }

    @Override
    protected void OnViewClicked(View v) {

    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

       /* if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        }*/

        if (mDrawer != null) {

            mDrawer.closeDrawer(GravityCompat.START);
        }
        return true;
    }

    /**
     * 初始化option GridView 数据
     */
    private void initGridData(OptionGroupData groupData) {
        View rawView = getLayoutInflater().inflate(R.layout.item_home_row_layout, null);
        TextView groupName = (TextView) rawView.findViewById(R.id.item_home_group_name);
        TempNestingGridView gridView = (TempNestingGridView) rawView.findViewById(R.id.item_home_chile_gridView);

        groupName.setText(TextUtils.isEmpty(groupData.getGroupName()) ? "" : groupData.getGroupName());
        List<OptionGroupData.OptionData> childData = groupData.getOptionData();
        OptionViewAdapter adapter = new OptionViewAdapter(childData, ActHome.this, R.layout.item_home_option_layout);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new GridClickListener(childData) {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                super.onItemClick(parent, view, position, id);
            }
        });
        act_home_rootView.addView(rawView);
    }

    public abstract class GridClickListener implements AdapterView.OnItemClickListener{
        List<OptionGroupData.OptionData> mItemData;
        public GridClickListener(List<OptionGroupData.OptionData> itemData) {
            mItemData = itemData;

        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                showToast("点击Action="+mItemData.get(position).getAction());
            switch (mItemData.get(position).getAction()){
                case "异常判断":
                    startActivity(new Intent(ActHome.this,ActExceptionJudgementList.class));//跳转到加工页面
                    break;
                case "产品加工":
                    startActivity(new Intent(ActHome.this,ActProduction.class));//跳转到加工页面
                    break;
                case "加工异常":
                    startActivity(new Intent(ActHome.this,ActExceptionJiaGongList.class));//跳转到加工页面
                    break;
                case "设备异常":
                    startActivity(new Intent(ActHome.this,ActExceptionEquList.class));//跳转到加工页面
                    break;
                case "刀具异常":
                    startActivity(new Intent(ActHome.this,ActExceptionDaoJuList.class));//跳转到加工页面
                    break;
            }
            /*if (position==0){

                startActivity(new Intent(ActHome.this,ActProduction.class));//跳转到加工页面
            } else if (position==1) {
                startActivity(new Intent(ActHome.this,ActExceptionJudgementList.class));//跳转到加工页面
            }*/
        }
    }
    //
    private class OptionViewHolder {
        private TextView name;
        private ImageView image;

        public ImageView getImage() {
            return image;
        }

        public void setImage(ImageView image) {
            this.image = image;
        }

        public TextView getName() {
            return name;
        }

        public void setName(TextView name) {
            this.name = name;
        }
    }

    public class OptionViewAdapter extends TempListAdapter<OptionGroupData.OptionData, OptionViewHolder> {
        public OptionViewAdapter(List data, Context context, int layoutId) {
            super(data, context, layoutId);
        }

        @Override
        protected OptionViewHolder createHolder() {
            return new OptionViewHolder();
        }

        @Override
        protected void initHolder(int position, View v, OptionViewHolder holder) {
            holder.setImage((ImageView) v.findViewById(R.id.item_home_option_child_image));
            holder.setName((TextView) v.findViewById(R.id.item_home_option_child_name));
        }

        @Override
        public void bunldHolderValue(int position, OptionViewHolder holder, OptionGroupData.OptionData item) {
            Glide.with(getTempContext()).load(item.getImage()).into(holder.getImage());
            holder.getName().setText(item.getName());
        }
    }
}
